package com.alex.utils.etc;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

@SuppressWarnings({"rawtypes", "unchecked"})
public class ParseFilmNames {

	public static void main(String[] args) throws Exception {
		Pattern pattern = Pattern.compile("</td> <td>([^<]+)</td> <td>");
		Collection<File> files = FileUtils.listFiles(new File("/tmp/movie"), new String[] {"html"}, false);
		Collection<String> res = new HashSet<String>();
		for (File file : files) {
			if (!file.getName().startsWith("1."))
				continue;
			String str = FileUtils.readFileToString(file);
			Matcher matcher = pattern.matcher(str);
			while (matcher.find()) {
				String line = matcher.group(1).trim();
				System.out.println(line);
				for (String item : line.split("/")) {
					item = item.replaceAll("[^a-zA-Z0-9а-яА-Я]", " ").replaceAll("\\s+", " ").trim();
					String[] words = item.split(" ");
					StringBuilder sb = new StringBuilder();
					for (String word : words) {
						if (word.length() > 1) {
							sb.append(word).append(" ");
						}
					}
					item = sb.toString().trim();
					if (!item.isEmpty())
						res.add(item);
				}
			}
			break;
		}
		System.exit(0);
		
		Collection<String> part = null;
		Collection<Collection> parts = new ArrayList<Collection>();
		for (String line : res) {
			if (part == null) {
				part = new ArrayList<String>();
				parts.add(part);
			}
			part.add(line);
			if (part.size() == 1000) {
				part = null;
			}
		}
			
		int n = 1;
		for (Collection p : parts) {
			FileUtils.writeStringToFile(new File("/home/alex/list" + n++ + ".txt"), StringUtils.join(p, "\n"));	
		}
		
	}
}
